<?php
/**
 * Yotaku Shop - yshop
 * @author Creatint<creatint@163.com>
 * @copyright www.yotaku.cn 2016
 */
namespace Admin\Model;
use Think\Model;
use Think\Page;
class GoodsModel extends Model{
    protected $error   =   '';
    protected $show   =   '';
    //
    //自动完成
    protected $_auto = array(
        array('status', 1, self::MODEL_INSERT),
        array('update_at', NOW_TIME, self::MODEL_BOTH),
        array('pubdate', NOW_TIME, self::MODEL_INSERT),
        array('created_at', NOW_TIME, self::MODEL_INSERT),
    );


    public function getGoods($params=null,$page=false){
        //初始化查询条件
        $map    =   array();
        if(!empty($params['title'])){
            $map['title']   =   array('like', '%'.(string)$params['title'].'%');
        }
        if(!empty($params['category'])){
            $map['category_id']   =   $params['category'];
        }
        if(!empty($params['price'])){
            $arr_tmp    =   explode(',',$params['price']);
            $map['price']   =   array('between',$arr_tmp);
            unset($arr_tmp);
        }
        if(!empty($params['sales'])){
            $arr_tmp    =   explode(',',$params['sales']);
            $map['sales']   =   array('between',$arr_tmp);
            unset($arr_tmp);
        }
        if(!empty($params['stock'])){
            $arr_tmp    =   explode(',',$params['stock']);
            $map['stock']   =   array('between',$arr_tmp);
            unset($arr_tmp);
        }
        if(!empty($params['status'])){
            $map['status']  =   (int)$params['status'];
        }
        if(!empty($params['category_id'])){
            $map['category_id']  =   (int)$params['category_id'];
        }

        $count  =   $this->where($map)->count();
        $Page   =   new Page($count,20);
        $Page->setConfig('wrap',array('<li>','</li>','<li class="active">'));
        $Page->setConfig('prev','«');
        $Page->setConfig('next','»');
        $this->show   =   $Page->show();

        $result = $this->where($map)->order("update_at desc")->limit($Page->firstRow.','.$Page->listRows)->select();
//        $result =   $this->where($map)->page($_GET['p'].',25')->order("update_at desc")->select();

        return $result;
    }
    public function getDetail($id,$field=true){
        $info   =   $this->field($field)->find($id);
        //trace($info,'model_info');
        if(!is_array($info)){
            $this->error    =   '商品不存在';
            return false;
        }
        return $info;
    }
    public function updateGoods($data=null){
        $data   =   $this->create($data);
        if(empty($data)){
            $this->error    =   '数据为空';
            dump("dd");
//            return false;
        }
        trace($data,'data');

        /* 添加或新增基础内容 */
        if(empty($data['id'])){ //新增数据
            $id = $this->add(); //添加基础内容
            if(!$id){
                $this->error = '新增商品内容出错！';
                return false;
            }
            $data['id'] = $id;
        } else { //更新数据
            $status = $this->save(); //更新基础内容
            if(false === $status){
                $this->error = '更新商品内容出错！';
                return false;
            }
        }
        //商品添加或更新完成
        return $data;
    }
    public function del($id){
        if(empty($id)){
            $this->error = '选择要删除的商品';
        }
        $map = array('id'=>array('IN',trim(implode(',',$id),',')));
        //$res = $this->where($map)->delete();
        if($this->where($map)->delete()){
            return true;
        }else{
            $this->error = '无此条数据';
            return false;
        }
    }
    public function getError(){
        return $this->error;
    }
    public function getShow(){
        return $this->show;
    }
}